<?php
namespace app\common\model\trends;

use app\common\model\User;
use think\Model;
use think\Db;

class Huati extends Model
{
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置

    // 表名
    protected $name = 'trends_huati';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function trends()
    {
        return $this->belongsTo('app\common\model\Trends', 'trends_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

	//获取用户信息
	public function getuser($trends,$user_id){
		$user = User::get($trends->user_id);
		$data = [];
		$data['nickname'] = $trends->is_name == 1?$user->anony:$user->nickname;
		$data['avatar'] = $user->avatar;
		//显示级别
		$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$trends->user_id,'circle_id'=>$trends->circle_id])->find();

		if($circle){
			$data['level'] = $circle['level'];
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
			$nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$trends->circle_id])->value('nickname');
			$data['level_name'] = $nickname?$nickname:$level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			if($trends->user_id == $user_id && $circle['identity'] < 3){
				$data['identity'] = $circle['identity'];
			}else{
				$data['identity'] = 0;
			}
		}else{
			$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$trends->user_id,'circle_id'=>1])->find();
			$data['level'] = $circle['level'];
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
			$data['level_name'] = $level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			$data['identity'] = 0;
		}
		return $data;
	}
}
